from django.contrib import admin


from . import models
# Register your models here.


class SKUAdmin(admin.ModelAdmin):


    def save_model(self, request, obj, form, change):

        obj.save()

        from celery_tasks.html.tasks import generate_static_sku_detail_html

        generate_static_sku_detail_html.delay(obj.id)



class SKUImageAdmin(admin.ModelAdmin):


    def save_model(self, request, obj, form, change):

        obj.save()

        sku_id = obj.sku.id

        from celery_tasks.html.tasks import generate_static_sku_detail_html

        generate_static_sku_detail_html.delay(sku_id)

        #设置默认图片
        if not sku_id.default_image_url:

            sku_id.default_image_url = obj.image.url

            sku_id.save()

    def delete_model(self, request, obj):

        sku_id = obj.sku.id

        obj.delete()

        from celery_tasks.html.tasks import generate_static_sku_detail_html

        generate_static_sku_detail_html.delay(sku_id)



class SKUSpecificationAdmin(admin.ModelAdmin):


    def save_model(self, request, obj, form, change):

        obj.save()

        from celery_tasks.html.tasks import generate_static_sku_detail_html

        generate_static_sku_detail_html.delay(obj.sku.id)


    def delete_model(self, request, obj):

        sku_id = obj.sku.id

        obj.delete()

        from celery_tasks.html.tasks import generate_static_sku_detail_html

        generate_static_sku_detail_html.delay(sku_id)



class GoodsCategoryAdmin(admin.ModelAdmin):

    """自定义站点管理类，监听保存和删除事件"""


    def save_model(self, request, obj, form, change):

        obj.save()

        from celery_tasks.html.tasks import generate_static_list_search_html
        #保存后立即取执行重新生成list.html静态文件
        generate_static_list_search_html.delay()


    def delete_model(self, request, obj):

        obj.delete()

        from celery_tasks.html.tasks import generate_static_list_search_html
        # 保存后立即取执行重新生成list.html静态文件
        generate_static_list_search_html.delay()



admin.site.register(models.Goods)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU, SKUAdmin)
admin.site.register(models.SKUSpecification, SKUSpecificationAdmin)
admin.site.register(models.SKUImage, SKUImageAdmin)
admin.site.register(models.GoodsCategory, GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel)